var ticket = {
    selectIndex:0,
    isJoinFirst:false,
    isRefundFirst:true,
    selectTicketId:'',
    selectTicketAId:'',
    selectTicketStatus:'',
    joinPage:'',
    refundPage:'',
    //报名列表
    getJoinList: function (page) {
        var pageId = 1;
        if(page){
            pageId = page.getCurrent();
        }
        var selectedActivityId = $("#join_activity_select").val();
        var searchCondition = $("#join_search_condition").val();
        var searchValue = $("#join_search_value").val();
        var searchDateStart = $("#join_search_date_start").val();
        var searchDateEnd = $("#join_search_date_end").val();
        var orderKey = "pay_time";
        var orderValue = $("#join_order_paytime").attr('data-order-value');

        var search = {
            activity_id: selectedActivityId,
            start_time: searchDateStart,
            end_time: searchDateEnd,
            condition: searchCondition,
            value: searchValue,
            order_key: orderKey,
            order_value: orderValue
        };

        baseAjax("/MyActivity/getAllJoinList/", {pageId: pageId, search:search},
            function(){
                var _result = this;
                var _el = $("#join_list");
                _el.html('');
                var data = _result.data.list;
                if (data != null && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        data[i].i = i;
                        var ticket_item = template("ticket_join_item", data[i]);
                        _el.append(ticket_item);
                    }
                    if(page) {
                        page.setPageCount(_result.data.pages);
                        page.filling(_result.data.currPage);
                    }else{
                        ticket.joinPage.setPageCount(_result.data.pages);
                        ticket.joinPage.filling(_result.data.currPage);
                    }
                    $("#join_count").text(_result.data.count);
                } else {
                    if (pageId == 1) {
                        _el.append('<div class="no-data-box"><img class="no-data-img" src="/Public/image/www/no-data.png" alt="这是图片" /><p class="no-data-title">这里什么也没有</p></div>');
                    }
                }
            }, function(){
                var _result = this;
                layer.msg(_result.msg);
            }
        );
    },
    // 拒绝列表
    getAllRefundList: function (page) {
        var pageId = 1;
        if(page){
            pageId = page.getCurrent();
        }
        var selectedActivityId = $("#refund_activity_select").val();
        var searchCondition = $("#refund_search_condition").val();
        var searchValue = $("#refund_search_value").val();
        var searchDateStart = $("#refund_search_date_start").val();
        var searchDateEnd = $("#refund_search_date_end").val();
        var orderKey = "pay_time";
        var orderValue = $("#join_order_paytime").attr('data-order-value');

        var search = {
            activity_id: selectedActivityId,
            start_time: searchDateStart,
            end_time: searchDateEnd,
            condition: searchCondition,
            value: searchValue,
            order_key: orderKey,
            order_value: orderValue
        };
        baseAjax("/MyActivity/getAllRefundList/", {pageId: pageId, search:search},
            function(){
                var _result = this;
                var _el = $("#refund_list");
                _el.html('');
                var data = _result.data.list;
                if (data != null && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        data[i].i = i;
                        var ticket_item = template("ticket_refund_item", data[i]);
                        _el.append(ticket_item);
                    }
                    if (page) {
                        page.setPageCount(_result.data.pages);
                        page.filling(_result.data.currPage);
                    } else {
                        ticket.refundPage.setPageCount(_result.data.pages);
                        ticket.refundPage.filling(_result.data.currPage);
                    }
                    $("#refund_count").text(_result.data.count);
                } else {
                    if (pageId == 1) {
                        _el.append('<div class="no-data-box"><img class="no-data-img" src="/Public/image/www/no-data.png" alt="这是图片" /><p class="no-data-title">这里什么也没有</p></div>');
                    }
                }
            }, function(){
                var _result = this;
                layer.msg(_result.msg);
            }
        );
    },
    //票券流程详情
    getPersonDetail: function(that, aid, ticketId){  //活动ID ，票券ID
        var _el = $(that);
        //$(this).parent(".t-tr").appen;
        var open = _el.attr('data-open');
        if(open == 0){
            baseAjax("/MyActivity/getPersonDetail/",{id: aid, ticketId:ticketId},
                function(){
                    var _result = this;
                    var ticket_detail = template("ticket_detail", _result.data);
                    _el.parents(".t-tr").after(ticket_detail);
                    _el.parents(".t-tr").css({'background-color':'#f8f8f8','border-bottom':'none'});
                    _el.attr('data-open', '1');
                    _el.text('收起');
                    _el.siblings().removeClass('fa-caret-down').addClass('fa-caret-up');
                }, function(){

                }
            );
        }else if(open == 1){
            _el.parents(".t-tr").next().remove();
            _el.parents(".t-tr").css({'background-color':'white','border-bottom':'1px solid #dedede'});
            _el.attr('data-open','0');
            _el.text('查看');
            _el.siblings().removeClass('fa-caret-up').addClass('fa-caret-down');

        }
    },
    //同意或拒绝用户参加活动
    activityJoinOpt: function (that, aid, ticketId, type, reason) {
        var _el = $(that);
        baseAjax("/MyActivity/activityJoinOpt/", {id: aid, ticketId: ticketId, type: type, msg: reason},
            function () {
                layerClose();
                var _result = this;
                layer.msg(_result.msg);
                var tid = _el.parents(".t-tr").attr('data-value');
                if (tid == ticketId) {
                    if (_result.msg == '同意报名成功') {
                        _el.parents(".t-tr").find('.td-status').html('<span class="td-status-green">有效</span>');
                    } else if (_result.msg == '拒绝报名成功') {
                        _el.parents(".t-tr").find('.td-status').html('<span class="td-status-grey">审核拒绝</span>');
                    }
                    _el.parents(".t-tr").find('.td-btns').html('<span>无</span>');
                }
            }, function () {
                var _result = this;
                layer.msg(_result.msg);
            }
        );
    },
    //同意或拒绝退票
    toRefundTicket: function (that, aid, ticketId, type, reason) {
        var _el = $(that);
        baseAjax("/MyActivity/toRefundTicket/", {id: aid, ticketId: ticketId, type: type, msg: reason},
            function () {
                layerClose();
                var _result = this;
                layer.msg(_result.msg);
                var tid = _el.parents(".t-tr").attr('data-value');
                if(tid == ticketId){
                    if(_result.msg == '已成功退票'){
                        _el.parents(".t-tr").find('.td-status').html('<span class="td-status-grey">已退票</span>');
                    }else if(_result.msg == '已成功拒绝退票'){
                        _el.parents(".t-tr").find('.td-status').html('<span class="td-status-grey">审核拒绝</span>');
                    }
                    _el.parents(".t-tr").find('.td-btns').html('<span>无</span>');
                }
            }, function () {
                var _result = this;
                layer.msg(_result.msg);
            }
        );
    },
    //批量同意报名
    batchJoinAgreeOpt: function () {
        var selectCheck = $("input[name='join_checkbox_select']:checked");
        var selectTicket = [];
        for(var i = 0; i < selectCheck.length; i++){
            var checkbox = selectCheck[i];
            selectTicket.push(checkbox.value);
        }
        if(selectTicket.length == 0){
            layer.msg("请选择记录");
            return;
        }

        var _html = "<div>" +
            "<p style='text-align: center;'>" +
            "<span style='color: #20A0FF;margin-bottom: 15px' class='fa fa-info-circle fa-3x'></span>" +
            "</p>" +
            "<span>确定要同意选中"+ selectTicket.length +"条记录的报名吗？</span>" +
            "</div>";

        layer.confirm(_html, {
            skin:'lay-delete',
            btn: ['确定','取消'],
            title:false,
            closeBtn:false

        }, function(){
            baseAjax("/MyActivity/activityBatchJoinOpt/", {ticketIds: selectTicket, type: 1, msg: ''},
                function () {
                    layerClose();
                    var _result = this;
                    layer.msg(_result.msg);
                    ticket.getJoinList();
                    ticket.cancelSelect();
                }, function () {
                    var _result = this;
                    layer.msg(_result.msg);
                }
            );
        });
    },
    //批量拒绝报名
    batchJoinRejectOpt: function () {
        var selectCheck = $("input[name='join_checkbox_select']:checked");
        var selectTicket = [];
        for(var i = 0; i < selectCheck.length; i++){
            var checkbox = selectCheck[i];
            selectTicket.push(checkbox.value);
        }
        if(selectTicket.length == 0){
            layer.msg("请选择记录");
            return;
        }
        var _html = $('.lay-refuse-box-1');
        $("#refuse_select_num").text(selectTicket.length + "条");
        layer.open({
            type: 1,
            skin: 'lay-refuse', //加上边框
            area: ['400px', 'auto'], //宽高
            content: _html,
            title:'拒绝原因',
            closeBtn:false,
            btn:['拒绝','取消'],
            yes:function () {
                var selected_reason = $("input[name=refuse_reason_1]:checked").val();
                var reason = "";
                if(selected_reason == 0){
                    reason = $("#refuse_textarea_1").val();
                }else if(selected_reason == 1){
                    reason = "抱歉，活动满员了";
                }else if(selected_reason == 2){
                    reason = "与活动设置的参加资格不符";
                }else if(selected_reason == 3){
                    reason = "填写的报名信息不真实";
                }else if(selected_reason == 4){
                    reason = "计划有变，活动暂停";
                }
                baseAjax("/MyActivity/activityBatchJoinOpt/", {ticketIds: selectTicket, type: 0, msg: reason},
                    function () {
                        layerClose();
                        var _result = this;
                        layer.msg(_result.msg);
                        ticket.getJoinList();
                        ticket.cancelSelect();
                    }, function () {
                        var _result = this;
                        layer.msg(_result.msg);
                    }
                );
            }
        });
    },
    //拒绝报名弹窗
    rejectJoinDialog: function(that, aid, ticketId) {
        $("#refuse_textarea_2").val("");
        var _html = $('.lay-refuse-box-2');
        layer.open({
            type: 1,
            skin: 'lay-refuse', //加上边框
            area: ['400px', 'auto'], //宽高
            content: _html,
            title: '拒绝原因',
            closeBtn: false,
            btn: ['拒绝', '取消'],
            yes: function () {
                var selected_reason = $("input[name=refuse_reason_2]:checked").val();
                var reason = "";
                if (selected_reason == 0) {
                    reason = $("#refuse_textarea_2").val();
                } else if (selected_reason == 1) {
                    reason = "抱歉，活动满员了";
                } else if (selected_reason == 2) {
                    reason = "与活动设置的参加资格不符";
                } else if (selected_reason == 3) {
                    reason = "填写的报名信息不真实";
                } else if (selected_reason == 4) {
                    reason = "计划有变，活动暂停";
                }
                ticket.activityJoinOpt(that, aid, ticketId, 0, reason);
            }
        });
    },
    //拒绝退款弹窗
    rejectRefundDialog: function(that, aid, ticketId){
        $("#refuse_textarea_3").val("");
        var _html = $('.lay-refuse-box-2-b');
        layer.open({
            type: 1,
            skin: 'lay-refuse', //加上边框
            area: ['400px', 'auto'], //宽高
            content: _html,
            title:'拒绝原因',
            closeBtn:false,
            btn:['拒绝','取消'],
            yes:function () {
                var selected_reason = $("input[name=refuse_reason_3]:checked").val();
                var reason = "";
                if (selected_reason == 0) {
                    reason = $("#refuse_textarea_3").val();
                } else if (selected_reason == 1) {
                    reason = "一旦售出，不可退款";
                } else if (selected_reason == 2) {
                    reason = "超过可退款时间";
                } else if (selected_reason == 3) {
                    reason = "已线下退款";
                }
                ticket.toRefundTicket(that, aid, ticketId, 0, reason);
            }
        });
    },
    //批量同意退款弹窗
    batchRefundAgreeOpt:function(){
        var selectCheck = $("input[name='refund_checkbox_select']:checked");
        var selectTicket = [];
        for(var i = 0; i < selectCheck.length; i++){
            var checkbox = selectCheck[i];
            selectTicket.push(checkbox.value);
        }
        if(selectTicket.length == 0){
            layer.msg("请选择记录");
            return;
        }
        var _html = "<div>" +
            "<p style='text-align: center;'>" +
            "<span style='color: #20A0FF;margin-bottom: 15px' class='fa fa-info-circle fa-3x'></span>" +
            "</p>" +
            "<span>确定要同意选中"+ selectTicket.length +"条记录的报名吗？</span>" +
            "</div>";

        layer.confirm(_html, {
            skin:'lay-delete',
            btn: ['确定','取消'],
            title:false,
            closeBtn:false

        }, function(){
            baseAjax("/MyActivity/batchRefundTicket", {ticketIds: selectTicket, type: 1, msg: ''},
                function(){
                    layerClose();
                    var _result = this;
                    layer.msg(_result.msg);
                    ticket.getAllRefundList();
                    ticket.cancelSelect();
                }, function(){
                    layerClose();
                    var _result = this;
                    layer.msg(_result.msg);
                }
            );
        });
    },
    //批量拒绝退款弹窗
    batchRefundRejectOpt: function(){
        var selectCheck = $("input[name='refund_checkbox_select']:checked");
        var selectTicket = [];
        for(var i = 0; i < selectCheck.length; i++){
            var checkbox = selectCheck[i];
            selectTicket.push(checkbox.value);
        }
        if(selectTicket.length == 0){
            layer.msg("请选择记录");
            return;
        }
        $("#refuse_textarea_4").val("");
        var _html = $('.lay-refuse-box-1-b');
        layer.open({
            type: 1,
            skin: 'lay-refuse', //加上边框
            content: _html,
            title:'拒绝原因',
            area: ['400px', 'auto'], //宽高
            closeBtn:false,
            success:function (layero, index) {
                $(layero).find('#refuse-batch-number').text(selectTicket.length);
            },
            btn:['拒绝','取消'],
            yes:function () {
                var selected_reason = $("input[name=refuse_reason_4]:checked").val();
                var reason = "";
                if (selected_reason == 0) {
                    reason = $("#refuse_textarea_4").val();
                } else if (selected_reason == 1) {
                    reason = "一旦售出，不可退款";
                } else if (selected_reason == 2) {
                    reason = "超过可退款时间";
                } else if (selected_reason == 3) {
                    reason = "已线下退款";
                }
                baseAjax("/MyActivity/batchRefundTicket", {ticketIds: selectTicket, type: 0, msg: reason},
                    function(){
                        layerClose();
                        var _result = this;
                        layer.msg(_result.msg);
                        ticket.getAllRefundList();
                        ticket.cancelSelect();
                    }, function(){
                        layerClose();
                        var _result = this;
                        layer.msg(_result.msg);
                    }
                );
            }
        });
    },
    orderList: function (that, listBox){
        var _val = $(that).attr('data-order-value');
        if (_val != "") {
            if (_val == 1) {
                $(that).attr('data-order-value', 2);
                $(that).find('.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-up');
            } else {
                $(that).attr('data-order-value', 1);
                $(that).find('.fa-caret-up').removeClass('fa-caret-up').addClass('fa-caret-down');
            }
        } else {
            $(that).attr('data-order-value', 1);
        }
        if (listBox == 'join') {
            ticket.getJoinList(ticket.joinPage)
        } else if (listBox == 'refund') {
            ticket.getAllRefundList(ticket.refundPage);
        }
    },
    cancelSelect:function () {
        $("input[name='checkAll']").attr('checked',false);
    }
};
